package managedbean.Employee;

import entity.Position;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javaclass.EmailValidator;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import session.stateless.EmployeeBeanLocal;
import session.stateless.PositionSessionBeanLocal;
import javax.faces.context.FacesContext; 
import javax.faces.application.FacesMessage; 
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;

@ManagedBean
@ViewScoped
public class AddEmployeeManagerBean implements Serializable {
    @EJB
    private EmployeeBeanLocal employeeSessionBeanLocal;
    @EJB
    private PositionSessionBeanLocal positionSessionBeanLocal;
    
    private long employeeId;
    private String username;
    private String password;
    private String employeeName;
    private String employeeNRIC;
    private String employeeEmail;
    private String employeeContactNumber;
    private String employeeAddress;
    private String position;
    private List<String> positions;
    
    public AddEmployeeManagerBean() {
    }

    public String getEmployeeAddress() {
        return employeeAddress;
    }
    public void setEmployeeAddress(String employeeAddress) {
        this.employeeAddress = employeeAddress;
    }

    public String getEmployeeContactNumber() {
        return employeeContactNumber;
    }
    public void setEmployeeContactNumber(String employeeContactNumber) {
        this.employeeContactNumber = employeeContactNumber;
    }

    public String getEmployeeEmail() {
        return employeeEmail;
    }
    public void setEmployeeEmail(String employeeEmail) {
        this.employeeEmail = employeeEmail;
    }

    public long getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(long employeeId) {
        this.employeeId = employeeId;
    }

    public String getEmployeeNRIC() {
        return employeeNRIC;
    }
    public void setEmployeeNRIC(String employeeNRIC) {
        this.employeeNRIC = employeeNRIC;
    }

    public String getEmployeeName() {
        return employeeName;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public EmployeeBeanLocal getEmployeeSessionBeanLocal() {
        return employeeSessionBeanLocal;
    }
    public void setEmployeeSessionBeanLocal(EmployeeBeanLocal employeeSessionBeanLocal) {
        this.employeeSessionBeanLocal = employeeSessionBeanLocal;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPosition() {
        return position;
    }
    public void setPosition(String position) {
        this.position = position;
    }

    
    
    public List<String> getPositions(){
        List<String> positions = new ArrayList<String>();
        for (Object o: positionSessionBeanLocal.getPositions())
        {
            Position temp_Position = (Position)o;
            positions.add(temp_Position.getDescription());
        }
        return positions;        
    }

    public void saveNewEmployee(ActionEvent event)
    {
        Long temp_positionId = positionSessionBeanLocal.getPosition(position);
        try 
        { 
            boolean deletedStatus = false;
            boolean activatedStatus = true;
            long attempt = -1;
            employeeId = employeeSessionBeanLocal.createNewEmployee(username, password, employeeName, employeeNRIC, employeeEmail, employeeContactNumber, employeeAddress, attempt, activatedStatus, temp_positionId, deletedStatus);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Add New Sale Lead Result: New Employee Saved Successfully (New employee ID is " + employeeId + ")", "")); 
        } 
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
    }
    
    public void validateUniqueUser(FacesContext context, UIComponent toValidate, Object value){
        
        String userName = (String) value;
        Long employeeIdAttempt = employeeSessionBeanLocal.getEmployeeId(userName); 
        if (employeeIdAttempt != null) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Username is not unique. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }    
    
    public void validateNRIC(FacesContext context, UIComponent toValidate, Object value){
        
        String NRIC = (String) value;
        String[] temp_array = {"S", "F", "T", "G"};
        ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(temp_array));
        String prefix = NRIC.trim().substring(0,1);
        boolean check = true;
        if (!arrayList.contains(prefix))
        {
            check = false;
        }

        if (NRIC.length() != 9)
            check = false;
        else{
            try
            {
                Integer t = (Integer.parseInt(NRIC.substring(1,8)));
            }
            catch (NumberFormatException nfe)
            {
                check = false;
            }
        }
        if (!check) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("NRIC is not correct. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }    
    
     public void validateEmail(FacesContext context, UIComponent toValidate, Object value){
        
        String email = (String) value;
        
        EmailValidator emailValidator = new EmailValidator();
        boolean check = emailValidator.validate(email);
        if (!check) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Email is not correct. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }   
     
     public void validateContactNumber(FacesContext context, UIComponent toValidate, Object value){
        
        String contactNumber = (String) value;
        boolean check = true;
        Integer temp = 0;
        int count = 1;
        try 
        {
            temp = Integer.parseInt(contactNumber);
        }
        catch (NumberFormatException nfe)
        {
            check = false;
        }
        
        int div = 10;
        while (temp / div > 0)
        {
            div *=10;
            count+=1;
        }
        if ((count > 15) || (count < 8))
        {
            check = false;
        }
        if (!check) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Contact number is not correct. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }        
       
}
